#!/bin/bash

set -ex

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"

cd "$SCRIPT_DIR" || exit 1

pushd .. >/dev/null

if ! which protoc-gen-grpc-swift &>/dev/null; then
	echo "protoc-gen-grpc-swift is missing. Make sure to compile it first and add it to your PATH."
	exit 1
fi

if ! [[ -d "swift/Sources/bentoml" ]]; then
	protoc -I . -I ./thirdparty/protobuf/src \
		--swift_out=swift/Sources --swift_opt=Visibility=Public \
		--grpc-swift_out=swift/Sources --grpc-swift_opt=Visibility=Public \
		bentoml/grpc/v1alpha1/service.proto
	protoc -I . -I ./thirdparty/protobuf/src \
		--swift_out=swift/Sources --swift_opt=Visibility=Public \
		--grpc-swift_out=swift/Sources --grpc-swift_opt=Visibility=Public \
		bentoml/grpc/v1/service.proto
fi

popd >/dev/null

swift run BentoServiceClient
